package cn.iocoder.yudao.module.nginxui.service.httpconfig;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.nginxui.controller.admin.httpconfig.vo.*;
import cn.iocoder.yudao.module.nginxui.dal.dataobject.httpconfig.HttpConfigDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.nginxui.dal.mysql.httpconfig.HttpConfigMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.nginxui.enums.ErrorCodeConstants.*;

/**
 * Nginx HTTP块主配置 Service 实现类
 *
 * @author HuangJunZhong
 */
@Service
@Validated
public class HttpConfigServiceImpl implements HttpConfigService {

    @Resource
    private HttpConfigMapper httpConfigMapper;
    @Resource
    private HttpConfigSyncService httpConfigSyncService;

    @Override
    @Transactional
    public Long createHttpConfig(HttpConfigSaveReqVO createReqVO) {
        // 1. 业务逻辑（校验、转换DO等）
        HttpConfigDO httpConfig = BeanUtils.toBean(createReqVO, HttpConfigDO.class);
        setDefaultValues(httpConfig);
        httpConfigMapper.insert(httpConfig);

        // 2. 同步到主配置
        httpConfigSyncService.syncToMainConfig(httpConfig.getConfigId());
        return httpConfig.getId();
    }

    // 更新HTTP配置
    @Override
    @Transactional
    public void updateHttpConfig(HttpConfigSaveReqVO reqVO) {
        // 1. 业务逻辑（校验、更新DO等）
        HttpConfigDO httpConfig = validateHttpConfigExists(reqVO.getId());
        BeanUtils.copyProperties(reqVO, httpConfig);
        httpConfigMapper.updateById(httpConfig);

        // 2. 同步到主配置
        httpConfigSyncService.syncToMainConfig(httpConfig.getConfigId());
    }

    // 删除HTTP配置
    @Override
    @Transactional
    public void deleteHttpConfig(Long id) {
        // 1. 业务逻辑（校验、删除等）
        HttpConfigDO httpConfig = validateHttpConfigExists(id);
        httpConfigMapper.deleteById(id);

        // 2. 同步到主配置
        httpConfigSyncService.syncToMainConfig(httpConfig.getConfigId());
    }

    @Override
        public void deleteHttpConfigListByIds(List<Long> ids) {
        // 删除
        httpConfigMapper.deleteByIds(ids);
        }


    private HttpConfigDO validateHttpConfigExists(Long id) {
        HttpConfigDO httpConfig = httpConfigMapper.selectById(id);
        if (httpConfig == null) {
            throw exception(HTTP_CONFIG_NOT_EXISTS);
        }
        return httpConfig;
    }

    @Override
    public HttpConfigDO getHttpConfig(Long id) {
        return httpConfigMapper.selectById(id);
    }

    @Override
    public PageResult<HttpConfigDO> getHttpConfigPage(HttpConfigPageReqVO pageReqVO) {
        return httpConfigMapper.selectPage(pageReqVO);
    }
    /**
     * 为HTTP配置设置默认值（当字段为null时填充）
     */
    private void setDefaultValues(HttpConfigDO httpConfig) {
        if (httpConfig == null) {
            return;
        }
        // 1. sendfile默认启用（1=on，0=off）
        if (httpConfig.getSendfile() == null) {
            httpConfig.setSendfile(1);
        }
        // 2. 长连接超时时间默认65秒
        if (httpConfig.getKeepaliveTimeout() == null) {
            httpConfig.setKeepaliveTimeout(65);
        }
        // 3. 客户端最大请求体大小默认1m
        if (httpConfig.getClientMaxBodySize() == null) {
            httpConfig.setClientMaxBodySize("1m");
        }
        // 4. 状态默认启用（1=启用，0=禁用）
        if (httpConfig.getStatus() == null) {
            httpConfig.setStatus(1);
        }
        // 5. 其他需要设置默认值的字段可在此补充
    }
}